package com.rbcs.reimb.model;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.rbcs.base.model.Project;
import com.rbcs.flow.model.Flow;
import com.systop.common.modules.dept.model.Dept;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.model.BaseModel;

/***
 * 报销申请表
 * @author ShangHua
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "reimbs")
public class Reimb extends BaseModel {
  //ID
	private Integer id;
	//报销单号
  private String singleNo;
	//报销单类型(0-本部门; 1-跨部门)
  private String singleType;
	//报销总金额
	private Double amount;
	//部门
	private Dept dept;
	//项目
	private Project project;
	//报销人
	private User reimber;
  //报销日期
	private Date createDate;
	// 审批状态[部门和财务的审批]
	private String audStatus;
	//出纳付款状态
	private String payStatus;
	//支付方式
	private String payType;
	//完全销账的借款单Ids
	private String totalBorrowIds;
	//完全销账的借款单Nos
	private String totalBorrowNos;
	//部分销账的借款单Ids
	private String partBorrowIds;
	//部分销账的借款单Nos
	private String partBorrowNos;
	//销账借款金额
	private Double borrowAmount;
	//余款
	private Double balance;
	//部分销账冲抵的借款金额
	private Double offsetAmount;
	
	private Double totalNoRepayAmounts;
	
	private List<Flow> flows =  new ArrayList<Flow>(0);
	
	private List<ReimbDetail> reimbDetail = new ArrayList<ReimbDetail>();
	
	/**
	 * 父报销单
	 */
	private Reimb parentReimb;
	
	/**
	 * 子报销单
	 */
	private List<Reimb> childReimbs = new ArrayList<Reimb>();

	@Id
	@GeneratedValue(generator = "hibseq")
	@GenericGenerator(name = "hibseq", strategy = "hilo")
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "reimber_id")
  public User getReimber() {
		return reimber;
	}

	public void setReimber(User reimber) {
		this.reimber = reimber;
	}
	
	public String getSingleNo() {
		return singleNo;
	}

	public void setSingleNo(String singleNo) {
		this.singleNo = singleNo;
	}

	@Column(name = "single_type", length = 10)
	public String getSingleType() {
		return singleType;
	}

	public void setSingleType(String singleType) {
		this.singleType = singleType;
	}
	
	@Column(precision = 10, scale = 2)
	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "dept_id")
	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}
	
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column(name = "aud_status")
	public String getAudStatus() {
		return audStatus;
	}

	public void setAudStatus(String audStatus) {
		this.audStatus = audStatus;
	}

	@Column(name = "pay_status", columnDefinition = "char(1)")
	public String getPayStatus() {
		return payStatus;
	}

	public void setPayStatus(String payStatus) {
		this.payStatus = payStatus;
	}

	@Column(name = "pay_type", length = 10)
	public String getPayType() {
		return payType;
	}

	public void setPayType(String payType) {
		this.payType = payType;
	}

	@Column(name = "offset_amount", precision = 10, scale = 2)
	public Double getOffsetAmount() {
		return offsetAmount;
	}

	public void setOffsetAmount(Double offsetAmount) {
		this.offsetAmount = offsetAmount;
	}
	
	@Column(name = "total_borrowIds")
	public String getTotalBorrowIds() {
		return totalBorrowIds;
	}

	public void setTotalBorrowIds(String totalBorrowIds) {
		this.totalBorrowIds = totalBorrowIds;
	}

	@Column(name = "total_borrowNos")
	public String getTotalBorrowNos() {
		return totalBorrowNos;
	}

	public void setTotalBorrowNos(String totalBorrowNos) {
		this.totalBorrowNos = totalBorrowNos;
	}

	@Column(name = "part_borrowIds")
	public String getPartBorrowIds() {
		return partBorrowIds;
	}

	public void setPartBorrowIds(String partBorrowIds) {
		this.partBorrowIds = partBorrowIds;
	}

	@Column(name = "part_borrowNos")
	public String getPartBorrowNos() {
		return partBorrowNos;
	}

	public void setPartBorrowNos(String partBorrowNos) {
		this.partBorrowNos = partBorrowNos;
	}

	@Column(name = "borrow_amount", precision = 10, scale = 2)
  public Double getBorrowAmount() {
		return borrowAmount;
	}

	public void setBorrowAmount(Double borrowAmount) {
		this.borrowAmount = borrowAmount;
	}

	@Column(name = "balance", precision = 10, scale = 2)
	public Double getBalance() {
		return balance;
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy="reimb")
	public List<Flow> getFlows() {
		return flows;
	}

	public void setFlows(List<Flow> flows) {
		this.flows = flows;
	}
	
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "reimb")
  public List<ReimbDetail> getReimbDetail() {
		return reimbDetail;
	}

	public void setReimbDetail(List<ReimbDetail> reimbDetail) {
		this.reimbDetail = reimbDetail;
	} 
	
	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumn(name = "project_id")
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
	
	@Transient
	public Double getTotalNoRepayAmounts() {
		return totalNoRepayAmounts;
	}

	public void setTotalNoRepayAmounts(Double totalNoRepayAmounts) {
		this.totalNoRepayAmounts = totalNoRepayAmounts;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_reimb_id")
	public Reimb getParentReimb() {
		return parentReimb;
	}

	public void setParentReimb(Reimb parentReimb) {
		this.parentReimb = parentReimb;
	}  
	
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "parentReimb")
	@OrderBy("createDate")
	public List<Reimb> getChildReimbs() {
		return childReimbs;
	}

	public void setChildReimbs(List<Reimb> childReimbs) {
		this.childReimbs = childReimbs;
	}  
	
	@Transient
	public Flow getTopFlow() {
		return (flows.isEmpty()) ? null : flows.get(0);
	}

}
